package com.foodorder.controller;

import com.foodorder.service.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件上传控制器
 */
@RestController
@RequestMapping("/api/files")
public class FileController {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 上传单个文件
     *
     * @param file 文件
     * @param folder 存储文件夹（可选）
     * @return 文件URL
     */
    @PostMapping("/upload")
    public ResponseEntity<Map<String, Object>> uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam(value = "folder", required = false) String folder) {
        try {
            String fileUrl = fileStorageService.uploadFile(file, folder);
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "文件上传成功");
            
            Map<String, String> data = new HashMap<>();
            data.put("url", fileUrl);
            data.put("filename", file.getOriginalFilename());
            data.put("size", String.valueOf(file.getSize()));
            
            response.put("data", data);
            
            return ResponseEntity.ok(response);
        } catch (IOException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", e.getMessage());
            errorResponse.put("data", null);
            
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }

    /**
     * 上传多个文件
     *
     * @param files 文件列表
     * @param folder 存储文件夹（可选）
     * @return 文件URL列表
     */
    @PostMapping("/uploads")
    public ResponseEntity<Map<String, Object>> uploadFiles(
            @RequestParam("files") List<MultipartFile> files,
            @RequestParam(value = "folder", required = false) String folder) {
        try {
            List<String> fileUrls = fileStorageService.uploadFiles(files, folder);
            
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "文件上传成功");
            response.put("data", fileUrls);
            
            return ResponseEntity.ok(response);
        } catch (IOException e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("code", 500);
            errorResponse.put("message", e.getMessage());
            errorResponse.put("data", null);
            
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }

    /**
     * 删除文件
     *
     * @param fileUrl 文件URL
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public ResponseEntity<Map<String, Object>> deleteFile(@RequestParam("fileUrl") String fileUrl) {
        boolean deleted = fileStorageService.deleteFile(fileUrl);
        
        Map<String, Object> response = new HashMap<>();
        
        if (deleted) {
            response.put("code", 200);
            response.put("message", "文件删除成功");
            response.put("data", true);
        } else {
            response.put("code", 500);
            response.put("message", "文件删除失败");
            response.put("data", false);
        }
        
        return ResponseEntity.ok(response);
    }
} 